﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Province_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Dim level As String = -1
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                getParent()
                hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If

    End Sub
    Protected Sub getParent()
        drlParent.Items.Clear()
        drlParent.DataValueField = "Id"
        drlParent.DataTextField = "Title"
        RecursiveFillTree(0)
        drlParent.Items.Insert(0, New ListItem("-Chọn danh mục địa danh cấp cha-", "0"))

    End Sub
    Private Sub RecursiveFillTree(ByVal parentID As Integer)
        level += 1
        'on the each call level increment 1
        Dim appender As New StringBuilder()

        For j As Integer = 0 To level - 1

            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;")
        Next
        If level > 0 Then
            appender.Append("|__")
        End If

        Dim data As New VSEntities
        Dim dv As List(Of Province) = (From q In data.Provinces Where q.ParentId = parentID Select q).ToList
        Dim i As Integer

        If dv.Count > 0 Then
            For i = 0 To dv.Count - 1
                drlParent.Items.Add(New ListItem(Server.HtmlDecode(appender.ToString() + dv.Item(i).Title.ToString()), dv.Item(i).Id.ToString()))
                RecursiveFillTree(Integer.Parse(dv.Item(i).Id.ToString()))
            Next
        End If

        level -= 1
        'on the each function end level will decrement by 1
    End Sub

    Protected Sub ShowData()
        Dim data As New VSEntities
        Dim p As Province = (From q In data.Provinces Where q.Id = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            txtCode.Text = IIf(IsNothing(p.Code) = True, "", p.Code)
            txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
            drlParent.SelectedValue = IIf(IsNothing(p.ParentId) = True, "", p.ParentId)

        End If
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim data As New VSEntities
        Dim p As Province = (From q In data.Provinces Where q.Id = hidID.Value).SingleOrDefault
        If Not p Is Nothing Then
            'DUC 10-09-2014 kiem tra Code va Name da ton tai chua loai chinh no khi sua
            Dim checkCode = (From q In data.Provinces Where q.Id <> hidID.Value And q.Code = txtCode.Text.Trim() Select q.Code).FirstOrDefault
            If checkCode Is Nothing Then
                Dim checkName = (From q In data.Provinces Where q.Id <> hidID.Value And q.Title = txtTitle.Text.Trim() Select q).FirstOrDefault
                If checkName Is Nothing Then
                    Try
                        p.Id = hidID.Value
                        p.Code = txtCode.Text.Trim
                        p.Title = txtTitle.Text.Trim()
                        If (p.ParentId <> drlParent.SelectedValue) Then
                            Dim parent As Integer = CInt(drlParent.SelectedValue)
                        End If
                        p.ParentId = CInt(drlParent.SelectedValue)
                        data.SaveChanges()
                        Insert_App_Log("Update Province:" & txtTitle.Text.Trim & "", Function_Name.Province, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                        Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='../../Page/Province/List.aspx?Id=" & drlParent.SelectedValue.ToString & "';", Me.Page, True)
                        'Response.Redirect("List.aspx?Id=" + drlParent.SelectedValue.ToString)
                    Catch ex As Exception
                        log4net.Config.XmlConfigurator.Configure()
                        log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                        Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
                    End Try
                Else
                    Excute_Javascript("Alertbox('địa danh tồn tại trong hệ thống.')", Me.Page, True)
                End If
            Else
                Excute_Javascript("Alertbox('Mã đã tồn tại trong hệ thống.')", Me.Page, True)
            End If
        End If
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region
End Class
